package com.bj.dljd.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bj.dljd.domain.PmsMenu;
import com.bj.dljd.domain.UserInfo;
import com.bj.dljd.mapper.PmsMenuMapper;
import com.bj.dljd.mapper.UserInfoMapper;
import com.bj.dljd.models.dto.UserLoginDto;
import com.bj.dljd.constant.TokenConstant;
import com.bj.dljd.domain.PmsOperator;
import com.bj.dljd.mapper.PmsOperatorMapper;
import com.bj.dljd.models.menus.MenuItem;
import com.bj.dljd.models.menus.SubMenu;
import com.bj.dljd.models.query.UserQuery;
import com.bj.dljd.service.PmsOperatorService;
import com.bj.dljd.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.bj.dljd.constant.TokenConstant.TOKEN_JWT;

@Service
public class PmsOperatorServiceImpl implements PmsOperatorService {

    @Resource
    private PmsOperatorMapper pmsOperatorMapper;
    @Resource
    private RedisUtil redisUtil;


    @Override
    public UserLoginDto userLogin(UserQuery userQuery) {
        UserLoginDto userLoginDto = new UserLoginDto();

        String loginName = userQuery.getLoginName();
        String loginPwd = userQuery.getLoginPwd();

        QueryWrapper<PmsOperator> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name",loginName);
        PmsOperator pmsOperator = pmsOperatorMapper.selectOne(wrapper);
        if (pmsOperator == null){
            userLoginDto.add("账号或密码错误");
            return userLoginDto;
        }
        String md5Hex = DigestUtil.md5Hex(loginPwd + pmsOperator.getSalt());
        if (!md5Hex.equals(pmsOperator.getLoginPwd())){
            userLoginDto.add("账号或密码错误");
            return userLoginDto;
        }
        String sid = pmsOperator.getId() + TokenConstant.TOKEN_KEY;
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",sid);
        String jwt = JWTUtil.createToken(map, TOKEN_JWT.getBytes());


        String s = TOKEN_JWT + pmsOperator.getId();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("jwt",jwt);
        map1.put("loginTime", DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        int add = redisUtil.add(s, map1);

        if (add == 1){
            BeanUtils.copyProperties(pmsOperator,userLoginDto);
            userLoginDto.setJwt(jwt);
            return userLoginDto;
        }

        userLoginDto.add("账号或密码错误");
        return null;
    }
    @Resource
    private PmsMenuMapper pmsMenuMapper;

    @Override
    public List<SubMenu> queryMenus(Long operatorId) {
        // 返回值
        List<SubMenu> subMenuList = new ArrayList<>();
        // 查询所有的subMenu 就是parentID= 0
        List<PmsMenu> pmsMenus = pmsMenuMapper.selectMenuByOperatorId(operatorId);
        // 遍历所有的Submenu 查询他每个
        for (PmsMenu pmsMenu : pmsMenus) {

            SubMenu subMenu = new SubMenu();
            subMenu.setId(pmsMenu.getId());
            subMenu.setName(pmsMenu.getName());
            subMenu.setNumber(pmsMenu.getNumber());
            subMenu.setUrl(pmsMenu.getUrl());


            List<MenuItem> menuItemList = new ArrayList<>();
            List<PmsMenu> pmsMenuList = pmsMenuMapper.selectMenuByParentId(pmsMenu.getId());

            for (PmsMenu menu : pmsMenuList) {
                MenuItem menuItem = new MenuItem();
                menuItem.setId(menu.getId());
                menuItem.setName(menu.getName());
                menuItem.setUrl(menu.getUrl());
                menuItem.setNumber(menu.getNumber());
                menuItemList.add(menuItem);
            }

            subMenu.setItems(menuItemList);
            subMenuList.add(subMenu);
        }
        return subMenuList;
    }
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> matchSopName(String shopName) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.like("shop_name",shopName)
                .orderByAsc("user_no");
        return userInfoMapper.selectList(wrapper);

    }
}
